package cn.boge.org.controller;

import cn.boge.org.domain.Department;
import cn.boge.org.query.DeptQuery;
import cn.boge.org.service.DepartmentService;
import cn.boge.org.utils.AjaxResult;
import cn.boge.org.utils.QueryResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(tags = "部门维护接口")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @PutMapping//添加或修改     @RequestBody接收前端给后端传递的json参数
    @ApiOperation(value = "参数是一个department对象")
    public AjaxResult addOrDel(@RequestBody Department department){
        try {
            if (department.getId()==null){
                departmentService.add(department);
            }else{
                departmentService.upt(department);
            }
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    @DeleteMapping("/{id}")//添加或修改     @RequestBody接收前端给后端传递的json参数
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            departmentService.del(id);
            AjaxResult ajaxResult = new AjaxResult();
            //System.out.println(ajaxResult);
            return ajaxResult;
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }

    @GetMapping//查询所有     @RequestBody接收前端给后端传递的json参数
    public List<Department> findAll(){
            return departmentService.findAll();
    }

    @GetMapping("/{id}")//查询单个
    public Department findById(@PathVariable("id") Long id){
        return departmentService.findById(id);
    }

    @PostMapping//根据条件查询
    public QueryResult<Department> findByQuery(@RequestBody DeptQuery Query){
        return departmentService.findByQuery(Query);
    }

    @PatchMapping//添加或修改     @RequestBody接收前端给后端传递的json参数
    public AjaxResult delpatch(@RequestBody List<Long> ids){
        try {
            departmentService.delpatch(ids);
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }
    @GetMapping("/deptTree")
    public List<Department>loadDeptTree(){
        return departmentService.loadDeptTree();
    }

}
